<?php

function compare($result2, $result1) {
      if($result1 == null) {
         if($result2 == null) {
            return 1;
         }
         
         return -1;
      }
      else if($result2 == null) {
         return -1;
      }

      if($result1->team == $result2->team) {
    	  if($result1->playerSurname == $result2->playerSurname) {
    		  return strcmp($result2->playerFirstName, $result1->playerFirstName);
    	  }
    	  else {
    		  return strcmp($result2->playerSurname, $result1->playerSurname);
    	  }
      }
      else {
    	  return strcmp($result2->team, $result1->team);
      }
}

function compareTeamPlayer($result2, $result1) {
      if($result1 == null) {
         if($result2 == null) {
            return 1;
         }
         
         return -1;
      }
      else if($result2 == null) {
         return -1;
      }

      if($result1->player->team == $result2->player->team) {
    	  if($result1->player->playerSurname == $result2->player->playerSurname) {
    		  return strcmp($result2->player->playerFirstName, $result1->player->playerFirstName);
    	  }
    	  else {
    		  return strcmp($result2->player->playerSurname, $result1->player->playerSurname);
    	  }
      }
      else {
    	  return strcmp($result2->player->team, $result1->player->team);
      }
}

function comparePlayerScore($result2, $result1) {
      if($result1 == null) {
         if($result2 == null) {
            return 1;
         }
         
         return -1;
      }
      else if($result2 == null) {
         return -1;
      }

      return $result1->score - $result2->score;
}

function comparePlayerScoreByAverageTotal($result2, $result1) {
      if($result1 == null) {
         if($result2 == null) {
            return 1;
         }
         
         return -1;
      }
      else if($result2 == null) {
         return -1;
      }

      return $result1->getAverageTotal() - $result2->getAverageTotal();
}

function comparePlayerScoreByPlayerType($result2, $result1) {
      if($result1 == null) {
         if($result2 == null) {
            return 1;
         }
         
         return -1;
      }
      else if($result2 == null) {
         return -1;
      }

      if ($result1->playerPosition == "RUCK") {
      	return 1;
      }
      else if ($result2->playerPosition == "RUCK") {
      	return -1;
      }
      else if ($result1->player->playerType == "KPP") {
      	return 1;
      }
      else if ($result2->player->playerType == "KPP") {
      	return -1;
      }
      else if ($result1->player->playerType == "HALF_KPP") {
      	return 1;
      }
      else if ($result2->player->playerType == "HALF_KPP") {
      	return -1;
      }
      else if ($result1->player->playerType == null) {
      	return 1;
      }
      else {
      	return -1;
      }
}

function comparePlayerScoreByAverageTop15($result2, $result1) {
      if($result1 == null) {
         if($result2 == null) {
            return 1;
         }
         
         return -1;
      }
      else if($result2 == null) {
         return -1;
      }

      return $result1->getAverageTop15() - $result2->getAverageTop15();
}

function comparePlayerScoreByAverageAbove30($result2, $result1) {
      if($result1 == null) {
         if($result2 == null) {
            return 1;
         }
         
         return -1;
      }
      else if($result2 == null) {
         return -1;
      }

      return $result1->getAverageAbove30() - $result2->getAverageAbove30();
}

function compareTeamResult($result1, $result2) {
      if($result1 == null) {
         if($result2 == null) {
            return 1;
         }
         
         return -1;
      }
      else if($result2 == null) {
         return -1;
      }
      if ($result1->againstValue == 0) {
      	return 0;
      }

      if($result1->gamesWon == $result2->gamesWon) {
    	  if($result1->gamesDrawn == $result2->gamesDrawn) {
    		  $result1Percentage = (floatval($result1->forValue) / floatval($result1->againstValue))*10000;
    		  $result2Percentage = (floatval($result2->forValue) / floatval($result2->againstValue))*10000;
    		  
    		  return intval($result2Percentage) - intval($result1Percentage); 
    	  }
    	  else {
    		  return $result2->gamesDrawn - $result1->gamesDrawn;
    	  }
      }
      else {
    	  return $result2->gamesWon - $result1->gamesWon;
      }
}

function compareAvailPlayers($result2, $result1) {
      if($result1 == null) {
         if($result2 == null) {
            return 1;
         }
         
         return -1;
      }
      else if($result2 == null) {
         return -1;
      }

      if($result1->playerSurname == $result2->playerSurname) {
    	  if($result1->playerFirstName == $result2->playerFirstName) {
    		  return strcmp($result2->team, $result1->team);
    	  }
    	  else {
    		  return strcmp($result2->playerFirstName, $result1->playerFirstName);
    	  }
      }
      else {
    	  return strcmp($result2->playerSurname, $result1->playerSurname);
      }
}

?>
